#include "protheus.ch"

USER FUNCTION ATF060GRV()
Local cAlias	:= Alias()
Local cQuery	:= ""

cQuery += "SELECT * FROM ( "
cQuery += "SELECT N3_CUSTBEM, N1_XCC, N1_XITEMCT, N3_SUBCCON "
cQuery += "FROM " + RetSqlName("SN1") + " SN1 "
cQuery += "JOIN " + RetSqlName("SN3") + " SN3 "
cQuery += "ON SN3.D_E_L_E_T_ = ' ' "
cQuery += "AND N3_FILIAL = N1_FILIAL "
cQuery += "AND N3_CBASE = N1_CBASE "
cQuery += "AND N3_ITEM = N1_ITEM "
cQuery += "WHERE SN1.D_E_L_E_T_ = ' ' "
cQuery += "AND N1_FILIAL >= ' ' "
cQuery += "AND N1_CBASE = '" + SN1->N1_CBASE + "' "
cQuery += "AND N1_ITEM = '" + SN1->N1_ITEM + "') "
cQuery += "WHERE N3_CUSTBEM <> N1_XCC "
cQuery += "OR N1_XITEMCT <> N3_SUBCCON "

DbUseArea(.t., "TOPCONN", TCGenQry(,, cQuery), "TBN1", .f., .f.)

IF !TBN1->(Eof())
	Reclock("SN1", .f.)
		SN1->N1_XCC		:= TBN1->N3_CUSTBEM
		SN1->N1_XITEMCT	:= TBN1->N3_SUBCCON
	MsUnlock()
ENDIF
TBN1->(DbCloseArea())

IF !Empty(Alltrim(cAlias)) .AND. Select(cAlias) > 0
	DbSelectArea(cAlias)
ENDIF
RETURN